package com.codedancer.campus.mapper;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.core.BaseMapper;
import com.codedancer.campus.po.UpdateLog;
import com.codedancer.campus.qo.updateLog.UpdateLogPageSearchQo;
import java.util.List;

public interface UpdateLogMapper extends BaseMapper<UpdateLog> {

    /**
     * 设置全部版本不置顶
     */
    default void updateNoTop() {
        LambdaUpdateWrapper<UpdateLog> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(UpdateLog::getIsTop, false);
        update(null, updateWrapper);
    }

    /**
     * 分页查询更新日志
     *
     * @param updateLogPage
     * @param updateLogPageSearchQo
     * @return
     */
    default IPage<UpdateLog> page(Page<UpdateLog> updateLogPage,
        UpdateLogPageSearchQo updateLogPageSearchQo) {
        LambdaQueryWrapper<UpdateLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(updateLogPageSearchQo.getVersion())) {
            lambdaQueryWrapper.like(UpdateLog::getVersion, updateLogPageSearchQo.getVersion());
        }
        lambdaQueryWrapper.orderByDesc(UpdateLog::getIsTop);
        return selectPage(updateLogPage, lambdaQueryWrapper);
    }

    /**
     * 获取最新创建的更新日志
     *
     * @return
     */
    default UpdateLog selectLastest() {
        LambdaQueryWrapper<UpdateLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(UpdateLog::getUpdateTime);
        List<UpdateLog> updateLogs = selectList(lambdaQueryWrapper);
        return CollUtil.isNotEmpty(updateLogs) ? updateLogs.get(0) : null;
    }

    /**
     * 获取置顶并且已推送的更新日志
     *
     * @return
     */
    default UpdateLog getTopAndPushed() {
        LambdaQueryWrapper<UpdateLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UpdateLog::getIsTop, true);
        lambdaQueryWrapper.eq(UpdateLog::getIsPush, true);
        return selectOne(lambdaQueryWrapper);
    }
}
